
var app = getApp();

Page({

    /**
     * 页面的初始数据
     */
    data: {
        erCode: '/images/scanning.png',
        selectIcon: '/images/select_icon.png',
        equipment_number: '', //主板编号
        s_number: '', //合伙人编号
        mainboardType: [], //获取到的主板主板类型
        mainboard_type_number: 999, //选中的主板主板类型
        mainboard_type_index: "",
        partners: [], //合伙人
        partnerIndex: '' //选择合伙人index
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.getMainboardType();
        this.getCurrentPartner();
    },

    //获取合伙人
    getCurrentPartner() {
        app.ajax({
            url: "Manage/Equipment/getPartnerList",
            success: res => {
                if (res.data.code > 0) {
                    this.setData({
                        partners: res.data.data
                    })
                } 
            }
        })
    },

    partnerChange(e) {
        this.setData({
            partnerIndex: e.detail.value
        })
    },

    //编辑主板编号
    editNumber(e) {
        this.setData({
            equipment_number: e.detail.value
        })
    },
    editSnumber(e) {
        this.setData({
            s_number: e.detail.value
        })
    },
    //获取下标
    contains(arr, obj) {
        var i = arr.length;
        while (i--) {
            if (arr[i].id === obj) {
                return i;
            }
        }
        return false;
    },
    //保存主板信息
    addLibrary() {
        var that = this;
        var url = 'Manage/';
        if (that.data.equipment_number == '') {
            app.showToast('请输入主板编号');
            return false;
        } else if (that.data.partnerIndex === '') {
            app.showToast("请选择合伙人");
            return false;
        } else if (that.data.mainboard_type_number == '') {
            app.showToast("请选择主板类型");
            return false;
        }
        app.ajax({
            url: url,
            data: {
                equipment_number: that.data.equipment_number,
                mainboard_type_number: that.data.mainboard_type_number,
                s_number: that.data.partners[that.data.partnerIndex].s_number,
            },
            success: function (res) {
                //console.log(res)
                if (res.data.code == 1000) {
                    app.showToast(res.data.message, "success");
                    setTimeout(() => {
                        const pages = getCurrentPages();
                        let targetPage = pages[pages.length - 2];
                        targetPage.reloadData();
                        wx.navigateBack();
                    }, 1500)
                } else {
                    app.showToast(res.data.message)
                }
            }
        })
    },
    //  主板类型选择
    mainboardChange: function (e) {
        var that = this;
        this.setData({
            mainboard_type_index: e.detail.value,
            mainboard_type_number: that.data.mainboardType[e.detail.value]['number']
        });
    },
    // 扫描二维码
    scan: function () {
        app.scanDeviceCode(true).then(res => {
            this.setData({
                equipment_number: res
            })
        }).catch(error => {
            app.showToast(error)
        })
        // var that = this;
        // wx.scanCode({
        //     success: (res) => {
        //         var newsp = res.result.split("=");
        //         if (newsp.length == 2 && newsp[1] != '') {
        //             that.setData({
        //                 equipment_number: newsp[1]
        //             })
        //         } else {
        //             app.showToast('扫码失败请重试或者手动输入', "none", 2000, function () {});
        //         }
        //     },
        //     fail: (res) => {
        //         app.showToast('扫码失败请重试或者手动输入', "none", 2000, function () {});
        //     }
        // })
    },
    //获取主板主板类型
    getMainboardType: function () {
        var that = this;
        app.ajax({
            url: 'Common/Common/getEquipmentMainboard',
            method: "POST",
            data: {},
            success: function (res) {
                if (res.data.code == 1000) {
                    var data = res.data.data;
                    that.setData({
                        mainboardType: data
                    })
                } else {
                    app.showToast(res.data.message);
                }
            }
        })
    },
})